package com.cskaoyan.service;

import com.cskaoyan.bean.bo.order.OrderBo;
import com.cskaoyan.bean.po.MarketChannel;
import com.cskaoyan.bean.po.MarketOrder;
import com.cskaoyan.bean.po.MarketOrderExample;
import com.cskaoyan.mapper.MarketOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:OrderServiceImpl
 * @Auther: HeXianZu
 * @Description:
 * @Date: 2022/10/12 09:37
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Override
    public List<MarketOrder> list(OrderBo orderBo) {
        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        Integer userId = orderBo.getUserId();
        String orderSn = orderBo.getOrderSn();
        Date start = orderBo.getStart();
        Date end = orderBo.getEnd();
        Short[] array = orderBo.getOrderStatusArray();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }
        if (array != null && array.length > 0) {
                criteria.andOrderStatusIn(Arrays.asList(array));
        }
        if (start != null && end != null) {
            criteria.andAddTimeBetween(start,end);
        }
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(example);
        return marketOrders;

    }

    @Override
    public MarketOrder orderDetail(Integer id) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(id);
        return marketOrder;
    }

    @Override
    public void ship(Map map) {

        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId((Integer) map.get("orderId"));
        marketOrder.setShipChannel((String) map.get("shipChannel"));
        marketOrder.setShipSn((String) map.get("shipSn"));
        marketOrder.setOrderStatus(((short) 301));
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    @Override
    public void refund(Map map) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId((Integer) map.get("orderId"));
        marketOrder.setOrderStatus((short) 202);
//        marketOrder.setRefundAmount(map.get("refundMoney"));
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    @Override
    public List<MarketChannel> channel() {
        List<MarketChannel> marketChannels = marketOrderMapper.selectChannel();
        return marketChannels;
    }
}
